﻿namespace WMS6.Web.Inbound
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InboundServiceRef;

    public class ReceiptInquiry : BasePage
    {
        protected Button btnAction;
        protected Button btnGoBack;
        protected Button btnRefresh;
        protected Button btnReport;
        protected DropDownList ddlReceiptType;
        protected DropDownList ddlStatus;
        protected HtmlForm F4041;
        protected GridView gvReceiptDetail;
        protected GridView gvReceiptHistory;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnReceiptID;
        protected HtmlHead Head1;
        protected Label lblASN;
        protected Label lblExceptReceiptDate;
        protected Label lblExternalReceiptID;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblPackingSlipNum;
        protected Label lblPOID;
        protected Label lblReceiptDate;
        protected Label lblReceiptID;
        protected Label lblReceiptType;
        protected Label lblRHTotalReceived;
        protected Label lblRHTotalReceivedQty;
        protected Label lblRMA;
        protected Label lblStatus;
        protected Label lblTotalCube;
        protected Label lblTotalExceptQty;
        protected Label lblTotalExpected;
        protected Label lblTotalExpectedQty;
        protected Label lblTotalGrossWeight;
        protected Label lblTotalReceiptQty;
        protected Label lblTotalReceived;
        protected Label lblTotalReceivedQty;
        protected Label lblVendorID;
        protected Label lblVendorRef;
        protected Label lblWaraHouseRef;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        private decimal RHTotalReceivedQty;
        protected ScriptManager ScriptManager1;
        private decimal totalExpectedQty;
        private decimal totalReceivedQty;
        protected TextBox txtExceptReceiptDate;
        protected TextBox txtExternalReceiptID;
        protected TextBox txtOwnerID;
        protected TextBox txtPackingSlipNum;
        protected TextBox txtPoID;
        protected TextBox txtReceiptDate;
        protected TextBox txtReceiptID;
        protected TextBox txtRMA;
        protected TextBox txtTotalCube;
        protected TextBox txtTotalExceptQty;
        protected TextBox txtTotalGrossWeight;
        protected TextBox txtTotalReceiptQty;
        protected TextBox txtVendorID;
        protected TextBox txtVendorRef;
        protected TextBox txtWaraHouseRef;
        protected UpdatePanel up1;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("ReceiptinquiryQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("ReceiptinquiryList.aspx?from=edit", false);
                    break;
            }
        }

        protected void gvReceiptDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F4041", this.gvReceiptDetail);
        }

        protected void gvReceiptDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as ReceiptInfo).ReceiptDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,3) </script>", false);
        }

        protected void gvReceiptDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvReceiptDetail.PageIndex * this.gvReceiptDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                if (((Label) e.Row.FindControl("lblExpectedQty")) != null)
                {
                    this.totalExpectedQty += decimal.Parse(((Label) e.Row.FindControl("lblExpectedQty")).Text);
                }
                if (((Label) e.Row.FindControl("lblReceivedQty")) != null)
                {
                    this.totalReceivedQty += decimal.Parse(((Label) e.Row.FindControl("lblReceivedQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalReceivedQty.Text = base.GetPreferenceDigit(this.totalReceivedQty);
                this.lblTotalExpectedQty.Text = base.GetPreferenceDigit(this.totalExpectedQty);
            }
        }

        protected void gvReceiptDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                ReceiptInfo receiptInfo = base.DataEntityViewState as ReceiptInfo;
                DataView dv = CADataConverter.ToDataTable(receiptInfo.ReceiptDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvReceiptDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,3);</script>", false);
            }
        }

        protected void gvReceiptHistory_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F4041", this.gvReceiptHistory);
        }

        protected void gvReceiptHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as ReceiptInfo).ReceiptHistoryInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',3,3) </script>", false);
        }

        protected void gvReceiptHistory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvReceiptHistory.PageIndex * this.gvReceiptHistory.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (((Label) e.Row.FindControl("lblReceivedQty")) != null)
                {
                    this.RHTotalReceivedQty += decimal.Parse(((Label) e.Row.FindControl("lblReceivedQty")).Text);
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblRHTotalReceivedQty.Text = base.GetPreferenceDigit(this.RHTotalReceivedQty);
            }
        }

        protected void gvReceiptHistory_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                ReceiptInfo receiptInfo = base.DataEntityViewState as ReceiptInfo;
                DataView dv = CADataConverter.ToDataTable(receiptInfo.ReceiptHistoryInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvReceiptHistory, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlReceiptType, "ReceiptType");
            base.BindData2DDLBySysCode(this.ddlStatus, "ReceiptStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnReceiptID.Value = base.Request.QueryString["ID"].ToString();
                this.SetReceiptInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetReceiptInfo2Form()
        {
            try
            {
                string receiptID = this.hdnReceiptID.Value.ToString();
                ReceiptInfo receiptInfo = InboundServiceClientProxy.GetInstance().GetReceiptInfoByID4Exec(receiptID);
                base.DataEntityViewState = receiptInfo;
                if (receiptInfo != null)
                {
                    this.txtReceiptID.Text = receiptInfo.ReceiptID;
                    this.ddlStatus.SelectedValue = receiptInfo.Status;
                    this.txtReceiptDate.Text = base.GetPreferenceShortDate(receiptInfo.ReceiptDate);
                    this.txtOwnerID.Text = receiptInfo.OwnerID;
                    this.ddlReceiptType.SelectedValue = receiptInfo.ReceiptType;
                    this.txtPoID.Text = receiptInfo.PoID;
                    this.txtExceptReceiptDate.Text = base.GetPreferenceShortDate(receiptInfo.ExpectedReceiptDate);
                    this.txtExternalReceiptID.Text = receiptInfo.ExternalReceiptID;
                    this.txtRMA.Text = receiptInfo.Rma;
                    this.txtPackingSlipNum.Text = receiptInfo.PackingSlipNumber;
                    this.txtWaraHouseRef.Text = receiptInfo.WarehouseRef;
                    base.BindData2GridView(this.gvReceiptDetail, receiptInfo.ReceiptDetailInfos);
                    base.BindData2GridView(this.gvReceiptHistory, receiptInfo.ReceiptHistoryInfos);
                    this.txtReceiptID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

